﻿Imports System.Data.OleDb

Public Class details
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


    

        
        Dim dr As OleDbDataReader
        Try
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("PlayersConnectionString").ConnectionString)
            '' opening the database connection
            oleDbConn.Open()
            '' fetching the data from platers table
            Dim TagExistCmd As OleDbCommand = New OleDbCommand("SELECT firstname+lastname FROM LawyersDetails Where ID=@F1", oleDbConn)
            TagExistCmd.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
            dr = TagExistCmd.ExecuteReader()
            dr.Read()
            TB_Title.Text = dr.GetValue(0)
        Catch ex As Exception
            TB_Title.Text = ""
        End Try

    End Sub

    

    Protected Sub AddTag_Click(ByVal sender As Object, ByVal e As EventArgs) Handles AddTag.Click



        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("PlayersConnectionString").ConnectionString)
        Dim Sql As String = "INSERT INTO LawyersTags (fk_LawyersDetails, Tag_fkID) VALUES (@f1,@f2)"
        '' opening the database connection
        oleDbConn.Open()

        For Each cb As ListItem In CheckBoxList1.Items
            If cb.Selected Then
                Dim cmd As OleDbCommand = New OleDbCommand(Sql, oleDbConn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
                cmd.Parameters.AddWithValue("@f2", cb.Value)
                cmd.ExecuteNonQuery()
            End If
        Next

        If TextBox1.Text = "" Then Return

        Dim newTags = TextBox1.Text.Split(",")

        For Each newTag In newTags

            Dim TagId As Double
            Dim dr As OleDbDataReader

            Try
                Dim TagExistCmd As OleDbCommand = New OleDbCommand("SELECT TagID FROM Tags Where Tag=@F1", oleDbConn)
                TagExistCmd.Parameters.AddWithValue("@f1", newTag)
                dr = TagExistCmd.ExecuteReader()
                dr.Read()
                TagId = dr.GetValue(0)
                '' to catch exception
            Catch ex As Exception
                TagId = 0
            End Try

            If TagId <= 0 Then
                ' then insert the tag into the tags table
                Dim newTagsql As String = "INSERT INTO Tags(Tag) VALUES(@f1)"
                Dim cmd2 As OleDbCommand = New OleDbCommand(newTagsql, oleDbConn)
                cmd2.CommandType = CommandType.Text
                cmd2.Parameters.AddWithValue("@f1", newTag)
                cmd2.ExecuteNonQuery()



            End If
        Next
        Response.Redirect("Details.aspx?ID=" & Request.QueryString("ID"))
    End Sub

    Protected Sub btn_submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_submit.Click

        Dim sql As String = "Insert into Comments (CTitle, Ctext, UserID, PlayerID, AddedDate) VALUES (@f1, @f2, @f3, @f4, NOW())"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("PlayersConnectionString").ConnectionString)
        Dim cmd3 As OleDbCommand = New OleDbCommand(sql, conn)
        cmd3.Parameters.AddWithValue("@f1", TB_Title.Text)
        cmd3.Parameters.AddWithValue("@f2", TB_Comment.Text)
        cmd3.Parameters.AddWithValue("@f3", User.Identity.Name)
        cmd3.Parameters.AddWithValue("@f4", Request.QueryString("ID"))
        '' opening the database connection
        conn.Open()
        cmd3.ExecuteNonQuery()

        conn.Close()
        cmd3.Dispose()
        conn.Dispose()

        Response.Redirect("Details.aspx?ID=" & Request.QueryString("ID"))
    End Sub

    Protected Sub TB_Title_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TB_Title.TextChanged

    End Sub

    
End Class